﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Net.Mail;
using System.Text;
using System.Threading.Tasks;

namespace SendMyZip.Classes
{
    public class Email
    {
        public static void Send(string emailFrom, string emailPassword, string host, string port,
            string sendTo, string subject, string body, string filePath = "")
        {
            try
            {
                var msg = new MailMessage();
                SmtpClient client;

                if (!string.IsNullOrWhiteSpace(filePath))
                {
                    var attach = new Attachment(filePath);
                    msg.Attachments.Add(attach);
                }
                msg.From = new MailAddress(emailFrom);
                msg.To.Add(new MailAddress(sendTo));
                msg.Subject = subject;
                msg.Body = body;
                msg.IsBodyHtml = true;
                msg.Priority = MailPriority.High;

                client = new SmtpClient(host);
                client.Port = Convert.ToInt32(port);
                client.EnableSsl = true;
                client.UseDefaultCredentials = true;
                client.Credentials = new NetworkCredential(emailFrom, emailPassword);

                client.Send(msg);
                client.Dispose();
                if (!string.IsNullOrWhiteSpace(filePath))
                {
                    msg.Attachments.Dispose();
                }

            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
    }
}
